package com.we.connect.weconnect.router.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.we.connect.weconnect.mapper.system.SysInfoMapper;
import com.we.connect.weconnect.model.remind.RemindInfo;
import com.we.connect.weconnect.model.remind.vo.RemindInfoVo;
import com.we.connect.weconnect.model.system.SendUserInfo;
import com.we.connect.weconnect.model.system.SysInfo;
import com.we.connect.weconnect.model.system.SysUser;
import com.we.connect.weconnect.model.system.vo.PasswordVo;
import com.we.connect.weconnect.service.RemindInfoService;
import com.we.connect.weconnect.service.SendUserInfoService;
import com.we.connect.weconnect.utils.RedisCache;
import com.we.connect.weconnect.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class AdmUserRouter {

    @Autowired
    private SysInfoMapper sysInfoMapper;

    @Autowired
    private SendUserInfoService sendUserInfoService;

    @Autowired
    private RemindInfoService remindInfoService;

    /**
     * 提醒页面
     * @param model
     * @return
     */
    @GetMapping("/remind")
    public String showRemindPage(Model model, HttpServletRequest request) {
        SysUser sysUser = (SysUser) UserUtils.getSysUser(request, "loginSysUser");
        List<RemindInfo> remindInfoVoList = remindInfoService.findRemindInfoVoList(sysUser.getUserId());
        model.addAttribute("remindInfoVoList", remindInfoVoList);
        return "admin/remind";
    }

    /**
     * 修改密码页面
     * @param model
     * @return
     */
    @GetMapping("/info")
    public String showInfoPage(Model model) {
        PasswordVo passwordVo = new PasswordVo();
        model.addAttribute("passwordVo", passwordVo);
        return "admin/info";
    }

    /**
     * 系统管理页面
     * @param model
     * @param request
     * @return
     */
    @GetMapping("/sysInfo")
    public String showSysInfoPage(Model model, HttpServletRequest request) {
        SysUser sysUser = (SysUser) UserUtils.getSysUser(request, "loginSysUser");
        QueryWrapper<SysInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", sysUser.getUserId());
        SysInfo sysInfo = sysInfoMapper.selectOne(wrapper);
        if(null == sysInfo){
            sysInfo = new SysInfo();
        }
        model.addAttribute("sysInfo", sysInfo);
        return "admin/sys-info";
    }

    /**
     * 用户列表
     * @param model
     * @param request
     * @return
     */
    @GetMapping("/userInfo")
    public String showUserInfoPage(Model model, HttpServletRequest request) {
        SysUser sysUser = (SysUser) UserUtils.getSysUser(request, "loginSysUser");
        List<SendUserInfo> sendUserInfoList = sendUserInfoService.getSendUserInfoList(sysUser);
        model.addAttribute("sendUserInfoList", sendUserInfoList);
        return "admin/user-info";
    }

    /**
     * 新增提醒页面
     * @param model
     * @return
     */
    @GetMapping("/addRemind/{functionName}/{id}")
    public String showAddRemindPage(Model model, HttpServletRequest request, @PathVariable("functionName") String functionName, @PathVariable("id") Long id) {
        SysUser loginSysUser = (SysUser) UserUtils.getSysUser(request, "loginSysUser");
        List<SendUserInfo> sendUserInfoList = sendUserInfoService.findSendUserInfoList(loginSysUser);
        model.addAttribute("sendUserInfoList", sendUserInfoList);
        switch (functionName){
            case "add": {
                RemindInfoVo remindInfoVo = new RemindInfoVo();
                model.addAttribute("remindInfoVo", remindInfoVo);
                model.addAttribute("message", "新增提醒");
                break;
            }
            case "edit": {
                RemindInfoVo remindInfoVo = remindInfoService.seeRemindInfo(id);
                model.addAttribute("remindInfoVo", remindInfoVo);
                model.addAttribute("message", "修改提醒");
                model.addAttribute("flag", 2);
                model.addAttribute("tableId", id);
                break;
            }
            case "see" : {
                RemindInfoVo remindInfoVo = remindInfoService.seeRemindInfo(id);
                model.addAttribute("remindInfoVo", remindInfoVo);
                model.addAttribute("message", "提醒详细");
                model.addAttribute("flag", 1);
                break;
            }
        }
        return "admin/add-remind";
    }

    /**
     * 新增或修改用户页面
     * @param model
     * @param functionName
     * @param id
     * @return
     */
    @GetMapping("/addUserInfo/{functionName}/{id}")
    public String showAddUserInfoPage(Model model, @PathVariable("functionName") String functionName, @PathVariable("id") Long id) {
        switch (functionName) {
            case "add": {
                SendUserInfo sendUserInfo = new SendUserInfo();
                model.addAttribute("sendUserInfo", sendUserInfo);
                model.addAttribute("functionName", "新增");
                break;
            }
            case "edit": {
                SendUserInfo sendUserInfo = sendUserInfoService.findSendUserInfo(id);
                model.addAttribute("sendUserInfo", sendUserInfo);
                model.addAttribute("functionName", "修改");
                model.addAttribute("id", id);
                break;
            }
        }
        return "admin/add-user-info";
    }

    /**
     * 系统帮助页面
     * @param model
     * @return
     */
    @GetMapping("/help")
    public String showHelpPage(Model model) {
        return "admin/help";
    }
}
